$(function() {

	var startDateDefault = moment().subtract('days', 6);// 7 days ago
	var endDateDefault = moment();
	var startWeekDefault = moment().startOf('month').weekday(0);
	var endWeekDefault = moment().endOf('month').weekday(6);
	var startMonthDefault = moment().startOf('year');
	var endMonthDefault = moment().endOf('year');
	var select = "Daily";
	drawTable(startDateDefault.format('YYYY/MM/DD'), endDateDefault
			.format('YYYY/MM/DD'));
	var cb = function(start, end, label) {
		$('#timeRange span').html(
				start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD'));
	};

	var optionSetDay = {
		startDate : startDateDefault,
		endDate : endDateDefault,
		dateLimit : {
			days : 31
		},
		showDropdowns : true,
		showWeekNumbers : true,
		timePicker : false,
		timePickerIncrement : 1,
		timePicker12Hour : true,
		ranges : {
			'Today' : [ moment(), moment() ],
			'Yestoday' : [ moment().subtract('days', 1),
					moment().subtract('days', 1) ],
			'Last 7 days' : [ moment().subtract('days', 6), moment() ],
			'Last 30 days' : [ moment().subtract('days', 29), moment() ],
			'This Month' : [ moment().startOf('month'), moment().endOf('month') ],
			'Last Month' : [ moment().subtract('month', 1).startOf('month'),
					moment().subtract('month', 1).endOf('month') ]
		},
		opens : 'left',
		buttonClasses : [ 'btn btn-default' ],
		applyClass : 'btn-small btn-primary',
		cancelClass : 'btn-small',
		format : 'YYYY-MM-DD',
		separator : ' to ',
		locale : {
			applyLabel : 'Apply',
			cancelLabel : 'Cancel',
			fromLabel : 'From',
			toLabel : 'To',
			customRangeLabel : 'Custom',
			daysOfWeek : [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
			monthNames : [ 'January', 'February', 'March', 'April', 'May',
					'June', 'July', 'August', 'September', 'October',
					'November', 'December' ],
			firstDay : 1
		}
	};

	$('#timeRange span').html(
			startDateDefault.format('YYYY/MM/DD') + ' - '
					+ endDateDefault.format('YYYY/MM/DD'));
	$('#timeRange').daterangepicker(optionSetDay, cb);
	$('#timeRange').on('apply.daterangepicker', function(ev, picker) {
		startPicker = picker.startDate.format('YYYY-MM-DD');
		endPicker = picker.endDate.format('YYYY-MM-DD');
		drawTable(startPicker, endPicker);
	});
	$('#timeRange').on('cancel.daterangepicker', function(ev, picker) {
		console.log("cancel event fired");
	});

	var cbWeek = function(start, end, label) {
		$('#weekRange span').html(
				start.weekday(0).format('YYYY/MM/DD') + ' - '
						+ end.weekday(6).format('YYYY/MM/DD'));
	};

	var optionSetWeek = {
		startDate : startWeekDefault,
		endDate : endWeekDefault,
		dateLimit : {
			days : 140
		},
		showDropdowns : true,
		showWeekNumbers : true,
		timePicker : false,
		timePickerIncrement : 1,
		timePicker12Hour : true,
		ranges : {
			'This Week' : [ moment().weekday(0), moment().weekday(6) ],
			'Last Week' : [ moment().subtract('days', 7).weekday(0),
					moment().subtract('days', 7).weekday(6) ],
			'This Month' : [ moment().startOf('month').weekday(0),
					moment().endOf('month').weekday(6) ],
		},
		opens : 'left',
		buttonClasses : [ 'btn btn-default' ],
		applyClass : 'btn-small btn-primary',
		cancelClass : 'btn-small',
		format : 'YYYY-MM-DD',
		separator : ' to ',
		locale : {
			applyLabel : 'Apply',
			cancelLabel : 'Cancel',
			fromLabel : 'From',
			toLabel : 'To',
			customRangeLabel : 'Custom',
			daysOfWeek : [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
			monthNames : [ 'January', 'February', 'March', 'April', 'May',
					'June', 'July', 'August', 'September', 'October',
					'November', 'December' ],
			firstDay : 1
		}
	};

	$('#weekRange span').html(
			startWeekDefault.format('YYYY/MM/DD') + ' - '
					+ endWeekDefault.format('YYYY/MM/DD'));
	$('#weekRange').daterangepicker(optionSetWeek, cbWeek);
	$('#weekRange').on('apply.daterangepicker', function(ev, picker) {
		startPicker = picker.startDate.format('YYYY-MM-DD');
		endPicker = picker.endDate.format('YYYY-MM-DD');
		drawWeekTable(startPicker, endPicker);
	});
	$('#weekRange').on('cancel.daterangepicker', function(ev, picker) {
		console.log("cancel event fired");
	});

	var cbMonth = function(start, end, label) {
		$('#monthRange span').html(
				start.format('YYYY/MM/DD') + ' - ' + end.format('YYYY/MM/DD'));
	};

	var optionSetMonth = {
		startDate : startMonthDefault,
		endDate : endMonthDefault,
		dateLimit : {
			years : 3
		},
		showDropdowns : true,
		showWeekNumbers : true,
		timePicker : false,
		timePickerIncrement : 1,
		timePicker12Hour : true,
		ranges : {
			'This Month' : [ moment().startOf('month'), moment().endOf('month') ],
			'Last Month' : [ moment().subtract('month', 1).startOf('month'),
					moment().subtract('month', 1).endOf('month') ],
			'This Year' : [ moment().startOf('year'), moment().endOf('year') ],
			'Last Year' : [ moment().subtract('year', 1).startOf('year'),
					moment().subtract('year', 1).endOf('year') ],
		},
		opens : 'left',
		buttonClasses : [ 'btn btn-default' ],
		applyClass : 'btn-small btn-primary',
		cancelClass : 'btn-small',
		format : 'YYYY-MM-DD',
		separator : ' to ',
		locale : {
			applyLabel : 'Apply',
			cancelLabel : 'Cancel',
			fromLabel : 'From',
			toLabel : 'To',
			customRangeLabel : 'Custom',
			daysOfWeek : [ 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' ],
			monthNames : [ 'January', 'February', 'March', 'April', 'May',
					'June', 'July', 'August', 'September', 'October',
					'November', 'December' ],
			firstDay : 1
		}
	};

	$('#monthRange span').html(
			startMonthDefault.format('YYYY/MM/DD') + ' - '
					+ endMonthDefault.format('YYYY/MM/DD'));
	$('#monthRange').daterangepicker(optionSetMonth, cbMonth);
	$('#monthRange').on('apply.daterangepicker', function(ev, picker) {
		startPicker = picker.startDate.format('YYYY-MM-DD');
		endPicker = picker.endDate.format('YYYY-MM-DD');
		drawMonthTable(startPicker, endPicker);
	});
	$('#monthRange').on('cancel.daterangepicker', function(ev, picker) {
		console.log("cancel event fired");
	});

	$("#type").selectpicker({
		style : 'btn',
		size : 8,
		showContent : false
	});

	$('#type').val('Daily');
	$('#type').selectpicker('refresh');
	$("#type").on(
			'change',
			function() {
				select = $(this).val();
				if (select == 'Daily') {
					$("#timeRange").show();
					$("#weekRange").hide();
					$("#monthRange").hide();
					drawTable(startDateDefault.format('YYYY-MM-DD'),
							endDateDefault.format('YYYY-MM-DD'));
				} else if (select == 'Weekly') {
					$("#timeRange").hide();
					$("#weekRange").show();
					$("#monthRange").hide();
					drawWeekTable(startWeekDefault.format('YYYY-MM-DD'),
							endWeekDefault.format('YYYY-MM-DD'));
				} else if (select == 'Monthly') {
					$("#timeRange").hide();
					$("#weekRange").hide();
					$("#monthRange").show();
					drawMonthTable(startMonthDefault.format('YYYY-MM-DD'),
							endMonthDefault.format('YYYY-MM-DD'));
				}
			});

	function drawTable(startDate, endDate) {
		$(".table thead tr").html("<th></th>");
		$(".table tbody").html("");
		var date = moment(startDate, 'YYYY-MM-DD');
		var eDate = moment(endDate, 'YYYY-MM-DD');
		eDate.add('days', 1);
		var theadContent;
		while (date.diff(eDate, 'days') < 0) {
			theadContent = date.format('YYYY-MM-DD');
			$(".table thead tr").append(
					"<th>" + theadContent + "&nbsp;&nbsp;&nbsp;&nbsp;</th>");
			date.add('days', 1);
		}

		$.ajax({
			url : window.basePath + "log/summaryDay",
			type : 'POST',
			data : {
				start : startDate,
				end : endDate
			},
			success : function(data) {
				writeDataDay(data);
			}

		});
	}

	function writeDataDay(data) {
		$(".table tbody").html("");
		var type = "";
		for ( var i = 0; i < data.length; i++) {
			var td = "";
			var k = 0;

			$(".table thead tr th").each(
					function() {
						if (k > 0) {
							var flag = 0;
							var thHtml = $(this).html().split("&nbsp;");
							for ( var j = 0; j < data[i].length; j++) {
								if (thHtml[0] == data[i][j].date) {
									var valueF = parseFloat(data[i][j].value);
									if (valueF > 0) {
										td += "<td class='warning'>"
												+ data[i][j].value + "%";
									} else {
										td += "<td>0";
									}

									td += "</td>";
									flag = 1;
								}
							}
							if (flag == 0) {
								td += "<td>0</td>";
							}

						}
						k = k + 1;
					});
			if (i == 0) {
				type = 'TOTAL';
			} else if (i == 1) {
				type = 'DFA';
			} else {
				type = 'FT';
			}
			$(".table tbody")
					.append(
							"<tr><td><strong>" + type + "</strong></td>" + td
									+ "<tr/>");
		}
	}

	function drawWeekTable(startDate, endDate) {
		$(".table thead tr").html("<th></th>");
		$(".table tbody").html("");
		var date = moment(startDate, 'YYYY-MM-DD');
		var eDate = moment(endDate, 'YYYY-MM-DD');
		eDate.add('days', 7);
		var theadContent;
		while (date.diff(eDate, 'days') < -7) {
			theadContent = date.week() + ' of ' + date.format('YYYY');
			$(".table thead tr").append(
					"<th>Week " + theadContent
							+ "&nbsp;&nbsp;&nbsp;&nbsp;</th>");
			date.add('days', 7);
		}
		$.ajax({
			url : window.basePath + "log/summaryWeek",
			type : 'POST',
			data : {
				start : startDate,
				end : endDate
			},
			success : function(data) {
				writeDataDay(data);
			}

		});
	}

	function drawMonthTable(startDate, endDate) {
		$(".table thead tr").html("<th></th>");
		$(".table tbody").html("");
		var date = moment(startDate, 'YYYY-MM-DD');
		var eDate = moment(endDate, 'YYYY-MM-DD');
		eDate.add('month', 1);
		var theadContent;
		while (date.diff(eDate, 'months') < 0) {
			theadContent = date.format('MMMM') + " " + date.format('YYYY');
			$(".table thead tr").append(
					"<th>" + theadContent + "&nbsp;&nbsp;&nbsp;&nbsp;</th>");
			date.add('months', 1);
		}
		$.ajax({
			url : window.basePath + "log/summaryMonth",
			type : 'POST',
			data : {
				start : startDate,
				end : endDate
			},
			success : function(data) {
				writeDataDay(data);
			}

		});
	}

});
